/*  C Program to convert decimal to binary using stack  */

#include<stdio.h>
#include<stdlib.h>
#define MAX 50

int isEmpty(int top, int stack_arr[]);
void push(int x, int *top, int stack_arr[]);
int pop(int *top, int stack_arr[]);
void DecToBin(int num);

int main()
{
        int num;
        printf("Enter an integer : ");
        scanf("%d",&num);
        printf("Binary Equivalent is : ");
        DecToBin(num);

        return 0;

}/*End of main()*/

void DecToBin(int num)
{
        int stack[MAX], top=-1, rem;
        while(num!=0)
        {
                rem = num%2;
                push(rem, &top, stack);
                num/=2;
        }
        while(top!=-1)
                printf("%d", pop(&top, stack));
        printf("\n");
}


void push(int x, int *top, int stack_arr[])
{
        if(*top == (MAX-1))
                printf("Stack Overflow\n");
        else
        {
                *top=*top+1;
                stack_arr[*top] = x;
        }
}/*End of push()*/

int pop(int *top, int stack_arr[])
{
        int x;
        if(*top == -1)
        {
                printf("Stack Underflow\n");
                exit(1);
        }
        else
        {
                x = stack_arr[*top];
                *top=*top-1;
        }
        return x;
}/*End of pop()*/

//c++
#include<iostream>
#include<stack>
using namespace std;
void dec_to_bin(int number) {
   stack<int> stk;
   while(number > 0) {
      int rem = number % 2; //take remainder
      number = number / 2;
      stk.push(rem);
   }
   while(!stk.empty()) {
      int item;
      item = stk.top();
      stk.pop();
      cout << item;
   }
}
main() {
   int num;
   cout << "Enter a number: ";
   cin >> num;
   dec_to_bin(num);
}

//c  https://programming.vip/docs/using-stack-to-realize-binary-conversion.html
void DTB(int num,Sqstack * Stack)
{
    char  num_b[33] ;  //Used to store the converted binary
    char * temp = num_b;
    while(num)  //Push 
    {
        Stack_Push(Stack,num%2);
        num /= 2;
    }
    while(!Stack_Empty(Stack))//Stack until stack is empty  
    {
        *temp = Stack_Pop(Stack) + '0';
        temp++; 
    }
    *temp = '\0';
    printf("The binary number corresponding to this number is:%s\n",num_b);
}

//c  https://www.w3resource.com/c-programming-exercises/stack/c-stack-exercise-17.php

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

// Stack data structure to store binary digits
struct Stack {
    int top;
    int array[MAX_SIZE];
};
// Initialize a new stack
struct Stack* newStack() {
    struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
    stack->top = -1;
    return stack;
}
// Check if the stack is empty
int is_Empty(struct Stack* stack) {
    return (stack->top == -1);
}

// Function to push an item to the stack
void push(struct Stack* stack, int item) {
    if (stack->top >= MAX_SIZE - 1) {
        printf("Error: Stack full!\n");
        return;
    }
    stack->array[++stack->top] = item;
}
// Function to pop an item from the stack
int pop(struct Stack* stack) {
    if (is_Empty(stack)) {
        printf("Error: Stack underflow\n");
        return -1;
    }
    return stack->array[stack->top--];
}
 // Convert decimal to binary using stack
void decimal_to_binary(int decimal) {
    // Create a new stack to store binary digits
    struct Stack* stack = newStack();
    // Push the remainder onto the stack after dividing the decimal number by 2
    while (decimal > 0) {
        int remainder = decimal % 2;
        push(stack, remainder);
        decimal /= 2;
    }
   // To get the binary equivalent of the digits, pop them from the stack and print them
    printf("Binary equivalent of the said number is: ");
    while (!is_Empty(stack)) {
        printf("%d", pop(stack));
    }
    printf("\n");
}
int main() {
    int dec_no;
    printf("Input a decimal number: ");
    scanf("%d", &dec_no);
    decimal_to_binary(dec_no);
    return 0;
}